<?php
namespace mobadmin\action;

use herosphp\bean\Beans;
use herosphp\core\Controller;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;
use herosphp\core\Loader;
use utils\weixin\JSSDK;

/**
 * @author yu
 */
class OfferAction extends CommonAction
{


    public function index(HttpRequest $request)
    {

        $day = $request->getParameter('day');

        if(!in_array($day,array('today','yesterday','b_yesterday'))){

            $day='today';

        }

        $this->page = 1;
        $this->pagesize = 15;

        if ($day == 'today') {

            $search_arr['start_time'] = $search_arr['end_time'] = date('Y-m-d');

        } elseif ($day == 'yesterday') {

            $search_arr['start_time'] = $search_arr['end_time'] = date("Y-m-d", strtotime("-1 day"));

        } elseif ($day == 'b_yesterday') {

            $search_arr['start_time'] = $search_arr['end_time'] = date("Y-m-d", strtotime("-2 day"));
        }

        if($this->getLoginUserId()){

            $search_arr['saas_user_id']=$this->getLoginUserId();
        }


        $service = Beans::get('offer.offer.service');
        $result = $service->getOfferList($this->pagesize, $this->page, $search_arr);


        $this->assign('day', $day);

        $this->assign('items', $result['items']);
        $this->assign("title", "历史报价");

        $this->setView("offer/offer_index");


    }

    /**
     * 获取该用户上一次报价的联系人信息
     * @param HttpRequest $request
     */
    public function add(HttpRequest $request)
    {

        $saas_user_id=$this->getLoginUserId();

        $service = Beans::get('offer.offer.service');

        $item=$service->getLastOfferContactInfo($saas_user_id);
        //自定义牌号，后带到添加报价页面
        $item['auto_add_brand']['data_id']= $request->getParameter('data_id');
        $item['auto_add_brand']['type_name']= $request->getParameter('type_name');
        $item['auto_add_brand']['product_name']= $request->getParameter('product_name');
        $item['auto_add_brand']['factory_name']= $request->getParameter('factory_name');
        $item['auto_add_brand']['brand_name']= $request->getParameter('brand_name');
        $item['auto_add_brand']['tpfb']= $request->getParameter('tpfb');


        $this->assign("item", $item);

        $this->assign("title", "报价中心");

        $this->setView("offer/offer_add");
    }

    public function detail(HttpRequest $request)
    {
        $offer_id = $request->getParameter('offer_id');
        $shareSuccessPanel = $request->getParameter('shareSuccessPanel');

        $service = Beans::get('offer.offer.service');

        if($service->getOffer($offer_id,$this->getLoginUserId())){

            //自定义牌号，后带到添加报价页面
            $item['auto_add_brand']['data_id']= $request->getParameter('data_id');
            $item['auto_add_brand']['type_name']= $request->getParameter('type_name');
            $item['auto_add_brand']['product_name']= $request->getParameter('product_name');
            $item['auto_add_brand']['factory_name']= $request->getParameter('factory_name');
            $item['auto_add_brand']['brand_name']= $request->getParameter('brand_name');
            $item['auto_add_brand']['tpfb']= $request->getParameter('tpfb');
            $this->assign("item", $item);

            $result['items'] = $service->getOfferProducts($offer_id);
            $result['head'] = $service->getOffer($offer_id);

            $this->assign('shareSuccessPanel', $shareSuccessPanel);
            $this->assign('result', $result);

            if(isset($result['items'][0])){

                $first_offer_decs=$result['items'][0]['product_name'].'/'.$result['items'][0]['factory_name'].'/'.$result['items'][0]['brand_name'].' '.del0($result['items'][0]['sales_price']).'元/吨';
                $this->assign('first_offer_decs', $first_offer_decs);

            }

            //获取微信分享token

            $weixin_config = getConfig('weixin_config');

            Loader::import('utils.weixin.JSSDK');

            $jssdk = new JSSDK($weixin_config['appid'], $weixin_config['appser']);

            $signPackage = $jssdk->GetSignPackage();

            $this->assign('offer_id', $offer_id);

            $this->assign('token', $signPackage);

            $this->assign("title", "报价详情");

            $this->setView("offer/offer_detail");

        }else{

            $jump_url="/offer/offer/detail?offer_id={$offer_id}";

            location($jump_url);

        }

    }


    /**
     * 添加报价
     * @param HttpRequest $request
     */
    public function do_add(HttpRequest $request)
    {

        $data = $request->getParameter('data');

        $service = Beans::get('offer.offer.service');

        if($data['company_name']==''){

            $data['company_name']='';
        }

        $offerHead = array(
            'contact' => $data['contact'],
            'contact_mobile' => $data['contact_mobile'],
            'contact_mobile2' => $data['contact_mobile2'],
            'company_name' => $data['company_name'],
            'delivery_place' => $data['delivery_place'],
            'views' =>1,
            'addtime' =>myNow(),
            'edittime' =>myNow(),
            'is_delete' =>0,
            'manager_id' =>1,
            'saas_user_id' =>$this->getLoginUserId(),
        );

        $offerProducts=json_decode($data['offer_products'],true);

        $offerId=$service->addAll($offerHead,$offerProducts);

        if($offerId){

            AjaxResult::ajaxResult('000', 'ok',array('offer_id'=>$offerId));
        }else{

            AjaxResult::ajaxResult('001', 'error');

        }
    }

}
